package com.matt.service.courseV2.impl;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.exception.BusinessException;
import com.matt.commons.page.PagerQuery;
import com.matt.domain.Children;
import com.matt.domain.comment.ExtCommentsSupport;
import com.matt.domain.courseV2.*;
import com.matt.domain.courseV2.po.VendorCourseRecordPO;
import com.matt.domain.courseV2.vo.VendorRecordVO;
import com.matt.foundation.model.ErrorCode;
import com.matt.foundation.utils.CopyUtils;
import com.matt.repository.ChildrenRepository;
import com.matt.repository.courseV2.*;
import com.matt.service.CommentsService;
import com.matt.service.courseV2.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class VendorRecordServiceImpl implements VendorRecordService {


    @Autowired
    private VendorCourseRecordRepository vendorCourseRecordRepository;

    @Autowired
    private VendorCourseRelRepository vendorCourseRelRepository;

    @Autowired
    private MilepostService milepostService;

    @Autowired
    private GrowthObservationService growingStoryService;

    @Autowired
    private ChildrenRepository childrenRepository;

    @Autowired
    private AbilityService abilityService;

    @Autowired
    private CommentsService commentsService;

    @Autowired
    private  VendorFabulousService  vendorFabulousService;

    //评论类型
    int resType = 2;

    @Override
    public void add(StaffDTO staffDTO, VendorRecordVO vendorRecordVO) {
        String[] ids = vendorRecordVO.getChildrenIds().split(",");
        //获取课程绑定的学习课程进度
        VendorCourseRel vendorCourseRel = vendorCourseRelRepository.findByVendorCourseId(vendorRecordVO.getVendorCourseId());
        //当没有关系时候，不需要添加记录
        if (Objects.isNull(vendorCourseRel)){
            return;
        }
        for (int i = 0; i < ids.length; i++) {
            //获取学生对象
            Children children = childrenRepository.getOne(ids[i]);
            if (Objects.isNull(children)){
                throw new BusinessException(ErrorCode.IS_TARGET, "学生对象不存在");
            }
            if (vendorRecordVO.getAbilityState()>0){
                String [] abilityIds = vendorCourseRel.getAbilityIds().split(",");
                //循环查询能力指标信息
                for (int j = 0; j < abilityIds.length  ; j++) {
                    abilityService.add(children.getId(),abilityIds[j],vendorRecordVO.getAttachments());
                }
            }

            if (vendorRecordVO.getMilepostState()>0){
                String [] milepostIds = vendorCourseRel.getMilepostIds().split(",");
                //循环查询能力指标信息
                for (int j = 0; j < milepostIds.length  ; j++) {
                    milepostService.add(children.getId(),milepostIds[j],vendorRecordVO.getAttachments());
                }
            }

            if (vendorRecordVO.getGrowingState()>0){
                String [] growingIds = vendorCourseRel.getGrowingIds().split(",");
                //循环查询能力指标信息
                for (int j = 0; j < growingIds.length  ; j++) {
                    milepostService.add(children.getId(),growingIds[j],vendorRecordVO.getAttachments());
                }
            }

            VendorCourseRecord vendorCourseRecord = new VendorCourseRecord();
            CopyUtils.copyProperties(vendorRecordVO,vendorCourseRecord);
            vendorCourseRecord.setStaffId(staffDTO.getId());
            vendorCourseRecord.setAreaId(staffDTO.getAreaId());
            vendorCourseRecord.setStaffName(staffDTO.getRealName());
            vendorCourseRecord.setStaffImg(staffDTO.getHeadImgUrl());
            vendorCourseRecordRepository.save(vendorCourseRecord);
        }
    }

    @Override
    public Page<VendorCourseRecordPO> list(String areaId, PagerQuery pagerQuery) {

        PageRequest pageRequest = PageRequest.of(pagerQuery.getPageNum(), pagerQuery.getPageSize());

        Page<VendorCourseRecord>  list  = vendorCourseRecordRepository.findAllByAreaId(areaId,pageRequest);

        List<VendorCourseRecordPO> list1 = new ArrayList<>();

        for (int i = 0; i < list.getContent().size() ; i++) {
            VendorCourseRecord vendorCourseRecord  = list.getContent().get(i);
            VendorCourseRecordPO vendorCourseRecordPO = new VendorCourseRecordPO();
            CopyUtils.copyProperties(vendorCourseRecord,vendorCourseRecordPO);
            //评论
            ExtCommentsSupport exto = commentsService.commentHostForResIdAndResType(vendorCourseRecord.getId(), resType);
            //点赞
            List<Fabulous> fabulousList = vendorFabulousService.resIdFindAll(vendorCourseRecord.getId());
            vendorCourseRecordPO.setExtCommentsSupport(exto);
            vendorCourseRecordPO.setFabulous(fabulousList);
            list1.add(vendorCourseRecordPO);
        }
        return new PageImpl(list1, PageRequest.of(pagerQuery.getPageNum(), pagerQuery.getPageSize()), list.getTotalElements());
    }
}
